package com.zr.gdms.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.gdms.dao.SdepartSelectStudentdao;
import com.zr.gdms.model.Student;
import com.zr.gdms.util.JDBCUtil;

public class SdepartSelectStudentimpl implements SdepartSelectStudentdao {

	@Override
	public List<Student> getSdepartSelStudent(int page, int rows) {
		// TODO Auto-generated method stub
		List<Student> st=new ArrayList<Student>();
		StringBuffer sq1=new StringBuffer();
		sq1.append(" select * from student limit ?,?");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sq1.toString());						
			pst.setInt(1, (page-1)*rows);//起始位置（页码-1)*条数
			pst.setInt(2, rows);//每页条数
			ResultSet rs=pst.executeQuery();
			while(rs.next()){
				Student ss=new Student();
				ss.setSid(rs.getInt("sid"));
				ss.setSname(rs.getString("sname"));
				ss.setSage(rs.getString("sage"));			
				ss.setSsex(rs.getString("ssex"));
				ss.setSdepart(rs.getString("sdepart"));
				ss.setStel(rs.getString("stel"));		
				ss.setSstate(rs.getString("sstate"));			
				st.add(ss);	
			}		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return st;
		
	}

	@Override
	public int getStudentsCount(){
		// TODO Auto-generated method stub
		int count=0;
		StringBuffer sq1=new StringBuffer();
		sq1.append(" SELECT COUNT(sid) AS scount");
		sq1.append(" from student");	
		Connection con=JDBCUtil.getConnection();
		try {
			PreparedStatement pst=con.prepareStatement(sq1.toString());	
			ResultSet rt=pst.executeQuery();
			if(rt.next()){
				count=rt.getInt("scount");
				
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return count;
		
	}
}
